import { Fragment } from '@/components/Fragment'; import { Link as AmplifyLink } from '@aws-amplify/ui-react'; import { Example, ExampleCode } from '@/components/Example'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { LinkDemo } from './demo'; import { DefaultLinkExample, ExternalLinkExample, LinkGlobalStylingExample, LinkStylePropsExample, LinkThemeExample, } from './examples'; ## Demo ## Usage Import the Link component. Link renders an anchor element `` by default, which accepts an `href` attribute that specifies the Link's destination. ```jsx file=./examples/DefaultLinkExample.tsx ``` ### External Links To create a Link which opens in a new tab, use the `isExternal` prop. Under the hood, `isExternal` sets `target="_blank"` and `rel="noopener noreferrer"` on the `` anchor element. ```jsx file=./examples/ExternalLinkExample.tsx ``` ### Routing Libraries You can use a Link with any React routing library that supports custom components. Below is an example using Link with React Router v5, in which the Link is passed to the `component` prop as a custom navigation component: ```jsx import { Link, Flex, Heading } from '@aws-amplify/ui-react'; import { BrowserRouter as Router, Link as ReactRouterLink, Routes, Route, } from 'react-router-dom'; function Home() { return Home; } function About() { return About; } function Users() { return Users; } function App() { return ( Home About Users } /> } /> } /> ); } export default App; ``` ## CSS Styling ```tsx file=./examples/LinkThemeExample.tsx ``` ### Target classes ### Global Styling To override styling on all Links, you can set the Amplify CSS variables or use the built in `.amplify-link` class. #### CSS Pseudo-classes To style the Link component in different states, you can use any of these four CSS Pseudo-classes: `:active`, `:focus`, `:hover` and `:visited`. ```css /* styles.css */ [data-amplify-theme] { --amplify-components-link-color: var(--amplify-colors-purple-80); --amplify-components-link-hover-color: var(--amplify-colors-purple-60); } /* OR */ .amplify-link { color: var(--amplify-colors-purple-80); } .amplify-link:hover { color: var(--amplify-colors-purple-60); } ``` ### Local Styling To override styling on a specific Link, you can use (in order of increasing specificity): a class selector or style props. _Using a class selector:_ My Custom Link ```css /* styles.css */ .link-local-styles { color: var(--amplify-colors-blue-80); font-weight: var(--amplify-font-weights-bold); } .link-local-styles:hover { color: var(--amplify-colors-blue-60); } .link-local-styles:active { color: var(--amplify-colors-green-80); } ``` ```jsx import './styles.css'; My Custom Link; ``` _Using style props:_ ```jsx file=./examples/LinkStylePropsExample.tsx ```